<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_kaoshi/model')->方法名();
*/
namespace jy_xiangqin;
defined('BY_JYA') or exit('error');
class kefu extends \plugin{
	public function gettcp($tcpid=0,$tomid=0,$workerid=0,$isfan=0){
		if(!$tcpid){
			if($tomid==$_SESSION['uid']){
				return 0;
			}
			$item=pdo_get('jy_xiangqin_tcp',array('tomid'=>$isfan?$_SESSION['uid']:$tomid,'workerid'=>$workerid,'mid'=>$isfan?$tomid:$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			if(!$item['id']){
				$item=pdo_get('jy_xiangqin_tcp',array('mid'=>$tomid,'workerid'=>$workerid,'tomid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			}
			if(!$item['id']){
				$item=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$isfan?$tomid:$_SESSION['uid'],
					'tomid'=>$isfan?$_SESSION['uid']:$tomid,
					'workerid'=>$workerid,
					'createtime'=>TIMESTAMP,
				);
				pdo_insert('jy_xiangqin_tcp',$item);
				$item['id']=pdo_insertid();
			}
		}else{
			$item=pdo_get('jy_xiangqin_tcp',array('uniacid'=>$_SESSION['uniacid'],'id'=>$tcpid));
		}
		return $item;
	}
	public function addchatlog($postdata=array(),$isfan=0){
		$chatmsg = trim($postdata['msg']);
		$type=$postdata['type'];
		if(empty($postdata['tcpid'])){
			$return['error'] = 1;
			$return['msg'] = '会话丢失！';
			exi($return);
		}
		if(empty($_SESSION['uid'])){
			$return['error'] = 1;
			$return['msg'] = '请先登录会员！';
			exi($return['msg'],'error');
		}
		if($type==4){
			$chatmsg=$postdata['image'];
		}
		if($type==7){
			$chatmsg=$postdata['file'];
		}
		if($type==8){
			$chatmsg=$postdata['goodsid'];
		}
		if(empty($chatmsg)){
			$return['error'] = 1;
			$return['msg'] = '请输入对话内容！';
			exi($return['msg'],'error');
		}
		if($type == 1){
			//预留
		}elseif($type == 3){
			
			//语言
		}elseif($type == 4){
			//图片
		}elseif($type == 7){
			//文件
				$fileurl=tomedia($postdata['msg']);
				$pathinfo=pathinfo($postdata['msg']);
				$fileicon=$pathinfo[extension];
				$filename=pdo_getcolumn('core_attachment',array('uniacid'=>$_SESSION['uniacid'],'attachment'=>$chatmsg),'title');
				$filename=$filename.'.'.$pathinfo[extension];
				if(strpos($fileicon,'xls')){
					$fileicon='icon-exl';
				}elseif(strpos($fileicon,'pd')){
					$fileicon='icon-pdf';
				}elseif(strpos($fileicon,'do')){
					$fileicon='icon-exl';
				}elseif(strpos($fileicon,'zip') || strpos($fileicon,'rar')){
					$fileicon='icon-zip';
				}else{
					$fileicon='icon-copy';
				}
				$filesize=filesize(ROOT_AT.'/'.$postdata['msg']);
				$filesize=price($filesize/1024,'int');
				$filesize .='kb';
				$fileinfo['filename']=$filename;
				$fileinfo['fileurl']=$fileurl;
				$fileinfo['filesize']=$filesize;
				$fileinfo['fileicon']=$fileicon;
		}elseif($type == 8){
			//礼品
			$goods=pdo_get('jy_xiangqin_liping',array('id'=>$chatmsg,'uniacid'=>$_SESSION['uniacid']),array('id','title','price','jifen','picurl'));
		}
		$tcp=pdo_get('jy_xiangqin_tcp',array('uniacid'=>$_SESSION['uniacid'],'id'=>$postdata['tcpid']));
		$postdata['tomid']=$tcp['mid']!=$_SESSION['uid']?$tcp['mid']:$tcp['tomid'];
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$isfan?$postdata['tomid']:$_SESSION['uid'],
			'tcpid'=>$postdata['tcpid'],
			'liao_bi'=>$postdata['liao_bi'],
			'msg'=>$chatmsg,
			'type'=>$type,
			'goods'=>ijson_encode($goods),
			'fileinfo'=>ijson_encode($fileinfo),
			'tomid'=>$isfan?$_SESSION['uid']:$postdata['tomid'],
			'createtime'=>TIMESTAMP
		);
		if($postdata['audiotime']){
			$data['audiotime'] = intval($postdata['audiotime']/1000);
		}
		pdo_insert('jy_xiangqin_tcplog',$data);
		$logid = pdo_insertid();
		$res['log']=array(
			'id'=>$logid,
			'mid'=>$_SESSION['uid'],
			'type'=>$type,
			'goods'=>$goods,
			'fileinfo'=>$fileinfo,
			'tcpid'=>$postdata['tcpid'],
			'liao_bi'=>$postdata['liao_bi'],
			'tomid'=>$postdata['tomid'],
			'msg'=>$chatmsg,
		);
		$res['log']['audioleng'] = $data['audiotime']>2?$data['audiotime']:2;
		return $res;
	}
}
?>